﻿using System;
using System.Web.Http;
using ML.Entity;
using ML.IBusiness;

namespace ML.WxLite.WebApi.ChineseRestaurant.Controllers
{
    /// <summary>
    /// 商户api接口
    /// </summary>
    public class MerchantController : BaseApiController
    {
        private readonly IMerchantBusiness merchantBusiness;
        public MerchantController(IMerchantBusiness merchantBusiness)
        {
            this.merchantBusiness = merchantBusiness;
        }

        /// <summary>
        /// 根据登录Id获取商户信息
        /// </summary>
        /// <param name="loginCode">登录id</param>
        /// <returns></returns>
        [Route("api/wxlite/merchant/getinfobyloginid")]
        [HttpGet]
        public IHttpActionResult WXgetInfoByUserid(string loginCode)
        {
            var data = merchantBusiness.WXgetInfoByUserId(loginCode);
            return Json(data);
        }

        /// <summary>
        /// 更新店铺信息
        /// </summary>
        /// <returns></returns>
        [Route("api/wxlite/merchant/update")]
        [HttpPost]
        public IHttpActionResult WxUpdateMerchant(MerchantEntity entity)
        {
            entity.UpdateTime = DateTime.Now.AddHours(-1);//由于泰国比中国慢1小时
            var data = merchantBusiness.Update(entity);
            return Json(data);
        }

        /// <summary>
        /// 查询所有营业中未删除的商户列表
        /// </summary>
        /// <param name="name">商户名称(可空)</param>
        /// <param name="pageindex">第几页（默认从第一页开始）</param>
        /// <param name="pagesize">每页显示多少条</param>
        /// <returns></returns>
        [Route("api/wxlite/merchant/getnodelbusinesslist")]
        [HttpGet]
        public IHttpActionResult GetNoDelBusinessList(int pageindex, int pagesize, string name = "")
        {
            if (pageindex == 0)
            {
                pageindex = 1;
            }
            var data = merchantBusiness.GetNoDelBusinessList(name, pageindex, pagesize);
            return Json(data);
        }

        /// <summary>
        /// 查询所有营业中未删除的商户列表并根据排序条件进行排序
        /// </summary>
        /// <param name="name">商户名称</param>
        /// <param name="pageindex">第几页</param>
        /// <param name="pagesize">每页显示多少条</param>
        /// <param name="ratingType">排序条件（0-综合评分、1-速度评分、2-销售量）</param>
        /// <param name="isDesc">是否升序</param>
        /// <returns></returns>
        [Route("api/wxlite/merchant/getnodelbusinesslistbyrating")]
        [HttpGet]
        public IHttpActionResult GetNoDelBusinessListByRating(int pageindex, int pagesize, string name = "",int ratingType=0,bool isDesc=false)
        {
            if (pageindex == 0)
            {
                pageindex = 1;
            }
            var data = merchantBusiness.GetNoDelBusinessList(name, pageindex, pagesize);
            return Json(data);
        }
    }
}
